﻿$(document).ready(function () {
    $("#ul-nav li:not(.nav-header)").eq(6).addClass("active");
    page.init();
});

var page = {
    url: "/handler/admin/auction.ashx?action=",

    init: function () {
        page.ajaxUploadObj = new AjaxUpload('upload', {
            action: "/handler/upload.ashx?action=uploadFile",
            autoSubmit: true,
            data: {},
            onSubmit: function (file, ext) {
                $("#btnAdd").unbind("click").click(function () { alert("Logo上传中，请等图片上传后再操作"); });
                this.disable();
            },
            onChange: function (file, ext) {
                $("#ulogo").val(file);
                this.enable();
            },
            responseType: 'json',
            onComplete: function (file, response) {
                if (response.error) {
                    alert(response.error);
                    $("#ulogo").val('');
                }
                else {
                    $("#ulogo").val(response.url);
                }
                $("#btnAdd").unbind("click").click(page.submit);
                this.enable();
            }
        });

        $("#btnAdd").click(page.submit);
    },

    submit: function () {
        var title = $.trim($("#utitle").val());
        if (title == "") {
            alert("请输入主题");
            $("#utitle").focus();
            return false;
        }

        var description = $.trim($("#udescription").val());
        if (description == "") {
            alert("请输入描述");
            $("#udescription").focus();
            return false;
        }

        var start = $("#start").val();
        if ($.trim(start) == "") {
            alert("请选择开始时间");
            $("#start").focus();
            return false;
        }

        var end = $("#end").val();
        if ($.trim(end) == "") {
            alert("请选择结束时间");
            $("#end").focus();
            return false;
        }

        var logo = $("#ulogo").val();
        if ($.trim(logo) == "") {
            alert("请输入logo url地址或者上传本地图片");
            $("#ulogo").focus();
            return false;
        }

        $.post(page.url + "add", { title: title, description: description, logo: logo, start: start, end: end }, function (data) {
            if (data.error) {
                alert(data.error);
                return false;
            }
            alert("添加成功");
            $('.form-horizontal input').val('');
            $("#udescription").val('');
        }, "json");
    }
};

